import React, { Component } from 'react';
import {messages} from "share/common";
import {Badge, Button, message, Modal} from "antd";
import financeViewService from "./expense-view.service";
import AdvancedTable from 'components/advanced-table'

class BatchDownload extends Component {

  componentWillReceiveProps(nextProps) {
    //切换tab时需要重新请求批次列表
    if (nextProps.tabKey === 'download') {
      this.advancedTableRef.getList();
    }
  }

  /**
   * 设置【生成pdf】【下载】【删除】按钮的loading状态
   * @param id: 批次号id
   * @param type: pdfLoading downloadLoading deleteLoading
   * @param isLoading: true false
   */
  setBtnLoading = (id, type, isLoading) => {
    const { loading } = this.state;
    loading[type][isLoading ? 'push' : 'delete'](id);
    this.setState({ loading });
  };

  //生成pdf
  generatePDF = (id) => {
    this.setBtnLoading(id, 'pdfLoading', true);
    financeViewService.generatePDF(id).then(() => {
      this.advancedTableRef.getList();
    }).catch(() => {
      this.setBtnLoading(id, 'pdfLoading', false)
    })
  };

  //下载批次
  downloadBatch = (id) => {
    this.setBtnLoading(id, 'downloadLoading', true);
    financeViewService.downloadBatch(id).then(res => {
      this.setBtnLoading(id, 'downloadLoading', false);
      window.open(res.data.fileURL)
    }).catch(() => {
      this.setBtnLoading(id, 'downloadLoading', false)
    })
  };

  //删除批次
  handleDeleteBatch = (id) => {
    this.setBtnLoading(id, 'deleteLoading', true);
    financeViewService.deleteBatch(id).then(() => {
      message.success(messages('common.delete.success', {name: ""})/*删除成功*/);
      this.setBtnLoading(id, 'deleteLoading', false);
      this.advancedTableRef.getList();
    }).catch(() => {
      this.setBtnLoading(id, 'deleteLoading', false)
    })
  };

  getColumns = loading => {
    return [{
      title: messages('financial-management.key113')/*批次号*/, dataIndex: 'expenseReportBatchId'
    }, {
      title: messages('financial-management.key592')/*报销单数量*/, dataIndex: 'actualBatchSize', width: 120
    }, {
      title: messages('financial-management.key8')/*创建人工号*/, dataIndex: 'employeeID'
    }, {
      title: messages('financial-management.key9')/*创建人姓名*/, dataIndex: 'fullName'
    }, {
      title: messages('common.column.status'/*状态*/), dataIndex: 'status', width: 120,
      render: value => {
        switch(value) {
          case 0:
            return <Badge status="default" text={messages('financial-management.key593')/*未生成*/} />;
          case 1:
            return <Badge status="processing" text={messages('financial-management.key594')/*生成中*/} />;
          case 2:
            return <Badge status="success" text={messages('financial-management.key595')/*已生成*/} />;
          case 3:
            return <Badge status="error" text={messages('financial-management.key596')/*生成失败*/} />;
        }
      }
    }, {
      title: messages('common.operation'), dataIndex: 'id', width: 300,
      render: (value, record) => {
        return (
          <div className="batch-operate">
            <Button
              type="primary"
              className="margin-right-10"
              loading={record.status === 1 || loading.pdfLoading.indexOf(record.expenseReportBatchId) > -1}
              onClick={() => this.generatePDF(record.expenseReportBatchId)}
            >
              {messages('financial-management.key597')/*生成PDF*/}
            </Button>
            <Button
              className="margin-right-10"
              disabled={record.status !== 2}
              loading={loading.downloadLoading.indexOf(record.expenseReportBatchId) > -1}
              onClick={() => this.downloadBatch(record.expenseReportBatchId)}
            >
              {messages('financial-management.key598')/*下载*/}
            </Button>
            <Button
              loading={loading.deleteLoading.indexOf(record.expenseReportBatchId) > -1}
              disabled={record.status === 1}
              onClick={() => {
                Modal.confirm({
                  title: messages('financial-management.key599')/*确认删除？*/,
                  content: messages('financial-management.key600')/*删除批次内的报销单可重新生成批次！*/,
                  onOk: () => this.handleDeleteBatch(record.expenseReportBatchId),
                })
              }}>
              {messages('common.delete')/*删除*/}
            </Button>
          </div>
        )
      }
    }]
  };

  advancedTableRef = null;

  state = {
    loading: {
      pdfLoading: [],
      downloadLoading: [],
      deleteLoading: []
    }
  };

  service = (page, size) => financeViewService.getBatchList({ page, size });

  handleGetData = (data) => {
    data && data.map(item => {
      item.status !== 1 && this.setBtnLoading(item.expenseReportBatchId, 'pdfLoading', false);
    })
  };

  render() {
    const { loading } = this.state;
    return (
      <AdvancedTable
        index
        title={messages('financial-management.key607')/*批次下载*/}
        columns={this.getColumns(loading)}
        onRef={ref => this.advancedTableRef = ref}
        service={this.service}
        rowKey="id"
        onGetData={this.handleGetData}
      >

      </AdvancedTable>
    );
  }
}

export default BatchDownload;
